<?php

class ScaraInfoForm extends BaseScaraForm
{
  const NAME = 'scara';
  
  public function configure()
  {
    $this->useFields(array('address', 'postal_code', 'remaining_aps'));
    
    $this->getWidgetSchema()->setLabels(array(
      'address'       => 'Adresa',
      'postal_code'   => 'Codul poştal',
      'remaining_aps' => 'Numărul de apartamente',
    ));
    
    $this->getWidgetSchema()->setNameFormat(self::NAME.'['.$this->getObject()->getId().']'.'[%s]');
    
    $postalCodeValidator = $this->getValidator('postal_code');
    $postalCodeValidator->setOption('min_length', sfConfig::get('app_min_postal_code'));
    $this->setValidator('postal_code', new sfValidatorAnd(
      array(
        $postalCodeValidator,
        new PostalCodeValidator()
      ),
      array('required'  => false)
    ));

    $this->getValidator('address')
        ->setOption('required', true);
    
    $this->getValidator('remaining_aps')
        ->setOption('required', true)
        ->setOption('min', 1)
        ->setOption('max', sfConfig::get('mod_apartamente_max_aps'));
  }
  
  public function bindAndSave(array $taintedValues)
  {
    // a se vedea setNameFormat()
    $taintedValues = $taintedValues[$this->getObject()->getId()];
    
    return parent::bindAndSave($taintedValues);
  }
}
